<%@page import="machine.Page"%>
<%@page import="machine.MongoDB"%>
<%@page import="machine.Pair"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="machine.QueryProcessor"%>
<%

    request.setCharacterEncoding("UTF-8");

    String query = request.getParameter("search");
    if (!session.getAttribute("query").equals(query)) {
        session.setAttribute("query", query);
        /**
         * chama método para calculo da query parametos: String
         * session.getAttribute("query"); double session.getAttribute("B");
         * double session.getAttribute("K"); double
         * session.getAttribute("avgDocumentLenght");
         *
         * retorna: ArrayList session.setAttribute("queryList", null); int
         * session.setAttribute("queryLenght", 0); long
         * session.setAttribute("queryTime", 0);
         */
        QueryProcessor qProcessor = new QueryProcessor(
                Double.parseDouble(session.getAttribute("K") + ""),
                Double.parseDouble(session.getAttribute("B") + ""),
                Integer.parseInt(session.getAttribute("docsQuantity") + ""),
                Double.parseDouble(session.getAttribute("avgDocumentLenght") + ""));

        long time = System.currentTimeMillis();
        qProcessor.process(query);
        time = System.currentTimeMillis() - time;
        session.setAttribute("queryTime", time);
        session.setAttribute("queryList", qProcessor.getQueryList());
        session.setAttribute("queryLenght", qProcessor.getQueryList().size());

    }

    String idxP = request.getParameter("idx");
    if (idxP != null) {
        session.setAttribute("idx", Integer.parseInt(idxP));
    } else {
        session.setAttribute("idx", 0);
    }
%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta charset="UTF-8">
        <title>Onquitá</title>
        <link rel="stylesheet" type="text/css" href="css/searchPage.css" />
        <link rel="shortcut icon" href="favicon.ico" />
    </head>
    <body>
        <div class="content">
            <div class="header">
                <div class="floatLeft">
                    <a href="index.jsp">
                        <img src="imgs/littleLogo.png" />
                    </a>
                </div>
                <div class="floatLeft">
                    <form action="" method="get" name="searchInputText">

                        <%
                            out.println("<input type=\"text\" size=\"60\" name=\"search\" class=\"littleInputSearch\"");

                            String search = request.getParameter("search");
                            if (search != null && search.trim().length() > 0) {
                                out.println(" value=\"" + search + "\"");
                                session.setAttribute("search", search);
                            }
                            out.println(" />");

                        %>  

                        <img src="imgs/littleLupa.png" class="submitSearch" onclick="javascript: submit()" />
                    </form>
                </div>
            </div>
            <div class="searchBody">
                <spam class="searchResult">

                    <%                        out.println("Página " + session.getAttribute("idx") + " de proximadamente " + session.getAttribute("queryLenght") + " resultados (" + ((double) (Long.parseLong(session.getAttribute("queryTime") + "")) / 1000.) + " segundos)");
                    %>

                </spam>
                <div class="searchContent">

                    <% for (int i = 0; i < (Integer.parseInt(session.getAttribute("queryLenght").toString())); i++) {
                            List queryList = (ArrayList<Pair>) session.getAttribute("queryList");
                            MongoDB db = new MongoDB();
                            db.connect();
                            Page document = db.getPage(Long.parseLong(((Pair) queryList.get(i)).getFirst().toString()));
                    %>

                    <div class="searchIten">
                        <% out.println("<a href=\"" + document.getUrl() + "\" class=\"title\">" + document.getTitle()); %></a><br />
                        <span class="url"><% out.println(document.getUrl()); %></span><br />
                    </div>

                    <%
                        }
                    %>
                </div>
                <div class="browser">
                    <table>
                        <tr>
                            <td>
                                <img src="imgs/browserHead.png" />
                            </td>

                            <%
                                int idx = Integer.parseInt(session.getAttribute("idx").toString()) - 1;
                                if (idx < 0) {
                                    idx = 0;
                                }

                                int len = (Integer.parseInt(session.getAttribute("queryLenght") + "") / 10) - idx;
                                if (len > 10) {
                                    len = 10;
                                }

                                for (int i = 0; i < len; i++) {
                                    out.println("<td><a href=\"?search=" + session.getAttribute("search")
                                            + "&idx=" + (idx + i) + "\">");
                                    out.println("<img src=\"imgs/browserLoop.png\" /></a></td>");
                                }

                            %>
                            <td>
                                <img src="imgs/browserTail.png" />
                            </td>
                        </tr>
                        <tr>
                            <td>

                                <%                                    out.println("<a href=\"?search=" + session.getAttribute("search") + "&idx=" + idx + "\">Anterior</a>");

                                %>

                            </td>

                            <%                                for (int i = 0; i < len; i++) {
                                    out.println("<td><a href=\"?search=" + session.getAttribute("search") + "&idx=" + (i + idx) + "\">" + (i + idx) + "</a></td>");
                                }
                            %>
                            <td>
                                <%                                    out.println("<a href=\"?search=" + session.getAttribute("search") + "&idx=" + (idx + 2) + "\">Proximo</a>");

                                %>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
            <div class="foot">

            </div>
        </div>
    </body>
</html>
